package com.zishi.venuesbooking;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import org.json.JSONException;

import java.io.IOException;


public abstract class BaseActivity extends AppCompatActivity {
    public Context mContext;
    public Bundle mBundle;
    private int volumeUpCount = 0;
    private long firstVolumeUpTime = 0; // 第一次音量+的时间
    private static final long TIME_LIMIT = 2000; // 5秒
    protected ImageButton playBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mBundle = savedInstanceState;
        setContentView(initLayout());

        initView();
        try {
            initData();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (playPauseKeyReceiver != null) {
//            unregisterReceiver(playPauseKeyReceiver);
//        }
    }

    abstract protected int initLayout();
    abstract protected void initView();
    abstract protected void initData() throws JSONException, IOException;
    public void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    public void showLongToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    public void showToastSync(String message) {
        Looper.prepare();
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    public void navigateTo(Class cls) {
        Intent in = new Intent(mContext, cls);
        startActivity(in);
    }
    public void navigateToWithBundle(Class cls, Bundle bundle) {
        Intent in = new Intent(mContext, cls);
        in.putExtras(bundle);
        startActivity(in);
    }

    public void navigateToWithFlag(Class cls, int flags) {
        Intent in = new Intent(mContext, cls);
        in.setFlags(flags);
        startActivity(in);
    }

    public void navigateToWithSlowAnimation(Class cls) {
        Intent in = new Intent(mContext, cls);
        startActivity(in);
        // 设置过渡动画
        overridePendingTransition(R.anim.fade_in_slow, R.anim.fade_out_slow);
    }

    protected void insertVal(String key, String val) {
        SharedPreferences sp = getSharedPreferences("sp_eyoyo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.commit();
    }

    protected String findByKey(String key) {
        SharedPreferences sp = getSharedPreferences("sp_eyoyo", MODE_PRIVATE);
        return sp.getString(key, "");
    }




    private void openSettingsToChangeLauncher() {
        showLongToast("打开系统设置界面");
        Intent intent = new Intent(Settings.ACTION_HOME_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private void closeApp() {
        // 关闭所有活动
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            finishAffinity();
        } else {
            // 兼容旧版本
            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        }
    }

    private void openDefaultLauncher() {
        // 打开默认桌面
        showLongToast("打开默认桌面");

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setPackage("com.android.launcher3");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); // 确保新任务
        startActivity(intent);
    }

}
